<?php
declare (strict_types=1);

namespace addons\V2\admin\model;

use app\common\model\Base;

class ProductCate extends Base
{
    protected $name = 'addons_mall_product_cate';

    public static function getAll()
    {
        try {

            $result = parent::order('sort', 'asc')->select()->toArray();
            return self::mergeMenu($result);

        } catch (\Exception $e) {
            return [];
        }
    }

    public static function getCate($id = 0)
    {
        return parent::where(['pid' => $id,'status' => 1])
            ->field("id,icon,title")->order('sort')->select();
    }

    public static function mergeMenu($cate, $pid = 0, $lvl = 0, $left_px = 0)
    {
        $arr = array();

        foreach ($cate as $v) {
            if ($v['pid'] == $pid) {
                $v['lvl'] = $lvl + 1;
                $v['left_px'] = $left_px + 0;//左边距
                $arr[] = $v;
                $arr = array_merge($arr, mergeMenu($cate, $v['id'], $lvl + 1, $left_px + 40));
            }
        }
        return $arr;
    }
}
